package com.hn.shopadmin.controller;

import com.hn.kiss.modules.sys.shopcommon.result.CommonResult;
import com.hn.shopadmin.dto.JursdictionAllRoleDTO;
import com.hn.shopadmin.service.RoleJurisdictionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotBlank;
import java.util.List;

@Api(tags = "RoleJurisdictionController", description = "权限分配管理接口")
@RestController
@RequestMapping("/juriRole")
@Validated
public class RoleJurisdictionController {

    @Autowired
    private RoleJurisdictionService roleJurisdictionService;

    @ApiOperation("所有权限信息和对应角色")
    @ApiImplicitParam(name = "roleCode", value = "角色代码", required = true, dataType = "String", paramType = "query")
    @GetMapping("/juriAll")
    public CommonResult<List<JursdictionAllRoleDTO>> juriAll(@NotBlank(message = "roleCode不能为空") String roleCode){
        List<JursdictionAllRoleDTO> jursdictionAllRoleDTOS = roleJurisdictionService.listJurisdictionAllRoleCode(roleCode);
        return CommonResult.success(jursdictionAllRoleDTOS);
    }


    @ApiOperation("分配权限")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "roleCode", value = "角色代码", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "jurisdictionIds", value = "权限ids,多个以','隔开", required = true, dataType = "String", paramType = "query")
    })
    @PostMapping("/allocation")
    public CommonResult allocation(@NotBlank(message = "roleCode不能为空")String roleCode,
                                   @NotBlank(message = "jurisdictionIds不能为空") String jurisdictionIds){

        String[] split = jurisdictionIds.split(",");
        int count = roleJurisdictionService.allocationRoleJurisdiction(roleCode, split);
        if (count > 0){
            return CommonResult.success("成功");
        }
        return CommonResult.failed("失败");
    }
}
